<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;

/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');
?>
<div class="user-default-index">




    <?php ?>
    <h3><?php echo $prodSpecName . '管理'; ?></h3>
    <div class="row">
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">全部客户</span>
                    <span class="info-box-number"><?php echo $dataProvider->getTotalCount(); ?></span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">已租客户</span>
                    <span class="info-box-number">93,139</span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">覆盖区域</span>
                    <span class="info-box-number"> <?php echo $regionCnt . '个'; ?>   </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">本日新增客户</span>
                    <span class="info-box-number"><?php echo $regionTodayCnt . '个'; ?> </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
    </div>
    <?php ?>
    <p>
        <?php
            $createUrl                     = [];
            $createUrl[]                   = 'create';
            $createUrl['attr_type_spec_id']   = $attr_type_spec_id;
            $createUrl['prod_spec_name'] = $prodSpecName;


            //创建区域逻辑
            // 如果创建
            echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , $createUrl, ['class' => 'btn btn-success']);
        ?>

    </p>

    <?php
        $prodAttrCodes=[];

       // Yii::trace("=====================              ============");
        //Yii::trace($dataProvider);


        // 增加展示的字段
        $columns = [];
        $i = 0;

        // 复选框
        $columns[$i]['class'] = 'yii\grid\CheckboxColumn';
        $columns[$i]['multiple'] = true;
        $columns[$i]['name'] = 'uid';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        $i++;
        // id
        $columns[$i]['attribute'] = 'id';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        $i++;
        //名称
        $columns[$i]['label'] = '客户名称';
        $columns[$i]['attribute'] = 'cust_name';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'cust_name', ['class'=>'form-control']);

        $i++; // 区域
        $columns[$i]['label'] = '持有楼盘';
        $columns[$i]['format'] = 'raw';
        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            return Html::a(count($model->prodInstBuildings), ['/prod/prodinst/classify', 'prod_spec_id'=>10001,   'own_cust_id'=> $model->id ], ['title' => "持有楼盘"]);

        };

        $i++; // 区域
        $columns[$i]['label'] = '持有房源';
        $columns[$i]['format'] = 'raw';
        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            return Html::a(count($model->prodInstRooms), ['/prod/prodinst/classify', 'prod_spec_id'=>10002,   'own_cust_id'=> $model->id ], ['title' => "持有房源"]);

        };




        $i++; // 区域
        $columns[$i]['label'] = '二级区域名称'. $regionTypes[3];
        //$columns[$i]['attribute'] = 'parentRegions.region_name';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['content']   = function($model)
        {
            global $regionTypes2;
            return $model->parentRegions->region_name  ;
        };

        $i++; // 区域
        $columns[$i]['label'] = '区域名称';
        //$columns[$i]['attribute'] = 'region_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['content']   = function($model)
        {
            global $regionTypes2;
            return $model->regions->region_name . $regionTypes2 [ $model->regions->region_type_id ]  ;
        };
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'region_name', ['class'=>'form-control']);


        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

                $i++;
                $columns[$i]['label'] = $prodAttr->attr_spec_name;
                $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
                $a = $prodAttr->attr_spec_id;

                $columns[$i]['value']   = function($model, $key, $index, $column)use ($a)
                {
                    if ($model->custExternalAttrs)
                    {
                        foreach ($model->custExternalAttrs  as $attrInst)
                        {
                            if ($a == $attrInst->attr_spec_id)
                            {
                                return $attrInst->content;
                            }
                        }
                    }

                    return  '未设置具体的值！';

                };


                $columns[$i]['options'] = ['style'=>'width:180px'];

        }

        $i++;
        //名称
        $columns[$i]['label'] = '创建人';
        $columns[$i]['attribute'] = 'create_staff_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            return $model->users->username  ;
        };


        $i++;
        //名称
        $columns[$i]['label'] = '更新时间';
        $columns[$i]['attribute'] = 'modify_date';
        $columns[$i]['options'] = ['style'=>'width:180px'];

        $i++;
        //操作选项
        $columns[$i]['class']   = 'app\common\grid\CustActionColumn';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['buttons'] =  [
            'view' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '查看', ['class' => "glyphicon fa fa-eye"]), ['view', 'id'=>$model->id,   'attr_type_spec_id'=> $model->cust_type_id,    'prod_spec_name' => $prodSpecName], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'update' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '编辑', ['class' => "glyphicon fa fa-edit"]), ['update', 'id'=>$model->id,   'attr_type_spec_id'=> $model->cust_type_id,    'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'delete' => function ($url, $model, $key)  {
                return Html::a(Html::tag('span', '删除', ['class' => "glyphicon fa fa-trash"]), ['delete', 'id'=>$model->id,   'attr_type_spec_id'=> $model->cust_type_id ], ['class' => "btn btn-xs btn-danger",'label'=>'删除产品', 'title' => '删除子产品']);
            },

            'create' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '订单生成', ['class' => "glyphicon fa fa-edit"]), ['/prod/order/create', 'cust_id'=>$model->id,       'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'生成一张订单', 'title' => '查看子产品']);
            },

        ];


        echo GridView::widget([
        'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
        'layout' => "{items}{summary}{pager}",
        'options'=>['id'=>'grid'],
        'columns' => $columns

        ]);


    ?>


</div>
